/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.book;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.book.BookOrderDao;
import com.boxin.ims.modules.wechat.entity.book.BookOrder;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 预定服务Service
 * @author Jakemanse
 * @version 2013-12-08
 */
@Component
@Transactional(readOnly = true)
public class BookOrderService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(BookOrderService.class);
	
	@Autowired
	private BookOrderDao bookOrderDao;
	
	public BookOrder get(Long id) {
		return bookOrderDao.findOne(id);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-12-27  下午10:21:10
	 * @function <p>  </p>
	 * @param storeId
	 * @return
	 */
	public List<BookOrder> findByStorefrontId( Long storeId) {
		
		DetachedCriteria dc = bookOrderDao.createDetachedCriteria();
		dc.createAlias("storefront", "storefront");
		dc.add(Restrictions.eq("storefront.id", storeId));
		dc.addOrder(Order.desc("id"));
		
		return bookOrderDao.find( dc);
	}
	
	
	
	public List<BookOrder> find(  BookOrder bookOrder) {
		DetachedCriteria dc = bookOrderDao.createDetachedCriteria();
		if (bookOrder.getUser()!=null && bookOrder.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", bookOrder.getUser().getId()));
		}
		
		if (bookOrder.getStorefront()!=null && bookOrder.getStorefront().getId()>0){
			dc.createAlias("storefront", "storefront");
			dc.add(Restrictions.eq("storefront.id", bookOrder.getStorefront().getId()));
		}
		
		if (StringUtils.isNotEmpty(bookOrder.getStatus())){
			dc.add(Restrictions.like("status",  bookOrder.getStatus()));
		}
		
		if (StringUtils.isNotEmpty(bookOrder.getName())){
			dc.add(Restrictions.like("name", "%"+bookOrder.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return bookOrderDao.find( dc);
	}
	
	
	
	public Page<BookOrder> find(Page<BookOrder> page, BookOrder bookOrder) {
		DetachedCriteria dc = bookOrderDao.createDetachedCriteria();
		if (bookOrder.getUser()!=null && bookOrder.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", bookOrder.getUser().getId()));
		}
		
		if (bookOrder.getStorefront()!=null && bookOrder.getStorefront().getId()>0){
			dc.createAlias("storefront", "storefront");
			dc.add(Restrictions.eq("storefront.id", bookOrder.getStorefront().getId()));
		}
		
		if (StringUtils.isNotEmpty(bookOrder.getStatus())){
			dc.add(Restrictions.like("status",  bookOrder.getStatus()));
		}
		
		if (StringUtils.isNotEmpty(bookOrder.getName())){
			dc.add(Restrictions.like("name", "%"+bookOrder.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return bookOrderDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(BookOrder bookOrder) {
		bookOrderDao.save(bookOrder);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		bookOrderDao.deleteById(id);
	}
	
}
